package com.carworld.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.carworld.config.BaseController;
import com.carworld.model.*;
import com.carworld.utils.DateUtils;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.TemplateData;
import com.jfinal.weixin.sdk.api.TemplateMsgApi;

public class DividendController extends BaseController {

	/**
	 * 根据日期查询收入
	 */
	public void getPageOfIncome() {
		// renderPageJson(DividendRecord.dao.getPageOfIncome("8b364212d07f4263b110937d21637b24",getPara("beginTime"),getPara("endTime")));
		renderPageJson(DividendRecord.dao.getPageOfIncome(getUserCookie(), getPara("beginTime"), getPara("endTime")));
	}

	/**
	 * 根据年月日查询收入
	 */
	public void getSumIncomeByTime() {
		String year = DateUtils.getCurrentYear(new Date());
		String month = DateUtils.getCurrentMonth(new Date());
		String day = DateUtils.getCurrentDate(new Date());
		Object yearIncome = DividendRecord.dao.getSumIncomeByTime(getUserCookie(), year).getList();
		Object monthIncome = DividendRecord.dao.getSumIncomeByTime(getUserCookie(), month).getList();
		Object dayIncome = DividendRecord.dao.getSumIncomeByTime(getUserCookie(), day).getList();
		System.out.println(yearIncome.toString() + "-----" + monthIncome.toString() + "-----" + dayIncome.toString());

		JSONObject jsonObject = getJsonObject();
		jsonObject.put("yearIncome", yearIncome);
		jsonObject.put("monthIncome", monthIncome);
		jsonObject.put("dayIncome", dayIncome);
		renderJson(jsonObject);
	}

	/**
	 * 今日分红收入
	 */
	public void getIncomeTody() {
		String day = DateUtils.getCurrentDate(new Date());
		Object dayIncome = DividendRecord.dao.getSumIncomeByTime(getUserCookie(), day).getList();
		System.out.println(dayIncome.toString() + "-----" + dayIncome.toString() + "-----" + dayIncome.toString());

		JSONObject jsonObject = getJsonObject();
		jsonObject.put("dayIncome", dayIncome);
		renderJson(jsonObject);
	}

	/**
	 * 分红跳转,一键登录
	 */
	public void getIncomeTodyCount() {
		String userId = getPara("shareholderId");
		setUserCookie(userId);
		render("/pages/page_dividend/dividendIndex.html");
	}

	// 提现完成页面跳转分红主页
	public void returndividendIndex() {
		render("/pages/page_dividend/dividendIndex.html");
	}

	// 跳转青铜会员注册
	public void returnVerifySharHolder() {
		render("/pages/page_dividend/verifyShareHolder.html");
	}

	// 个人体现列表
	public void takeCash() {
		render("/pages/page_dividend/takeCashList.html");
	}

	// 分红体现列表
	public void inComels() {
		render("/pages/page_dividend/dividendRecord.html");
	}

	// 人脉列表
	public void teamls() {
		render("/pages/page_dividend/team.html");
	}

	// 套餐
	public void combols() {
		render("/pages/page_userCard/myVipCard.html");
	}

	// 分红页面初始化方法
	public void getDiList() {
		String userId = getUserCookie();//
		SysUser user = SysUser.dao.findByIdInCache(userId);

		BigDecimal tCashCount = UserWithdrawal.dao.findLastestByIdInCache(userId).get("withdrawls");// 获取提现总数;
		BigDecimal divRecordCount = DividendRecord.dao.getDividendSum(userId).get("sumIncome");// 分红总数
		BigDecimal tCashToday = DividendRecord.dao.getDividendByOrder(userId).get("sumIncome");// 当日分红总数
		BigDecimal ownCash = divRecordCount.subtract(tCashCount);// 账户可提现余额

		JSONObject jsonObject = getJsonObject();
		jsonObject.put("tCashCount", tCashCount);
		jsonObject.put("divRecordCount", divRecordCount);
		jsonObject.put("tCashToday", tCashToday);
		jsonObject.put("ownCash", ownCash);
		jsonObject.put("user", user);
		if (user != null) {
			jsonObject.put("phonenum", user.getPhone());
			jsonObject.put("photo", user.getUserPhoto());
		}
		renderJson(jsonObject);
	}

	// 分红详情列表初始化
	public void getdividendListByUserId() {
		String userId = getUserCookie();//
		String countNum = DividendRecord.dao.getCountDiByUserId(userId);
		List<DividendRecord> diList = DividendRecord.dao.getdividendListByUserId(userId, 0);
		JSONObject jsonObject = getJsonObject();
		jsonObject.put("diList", diList);
		jsonObject.put("countNum", countNum);
		renderJson(jsonObject);
	}

	// 分红详情列表初始化
	public void getdividendListByUserId2() {
		String userId = getUserCookie();
		int indexNum = getParaToInt("indexNum");
		List<DividendRecord> diList = DividendRecord.dao.getdividendListByUserId(userId, indexNum * 15);
		indexNum = indexNum + 1;
		JSONObject jsonObject = getJsonObject();
		jsonObject.put("diList", diList);
		jsonObject.put("indexNum", indexNum);
		renderJson(jsonObject);
	}

	// 分红审批成功通知接口
	public void sendDividendSuccessMsg() {
		String orderId = getPara("orderId");
		String dividendGDTotal = getPara("dividendGDTotal");
		System.out.println("进入通知分红审批成功方法");
		ShopOrder order = ShopOrder.dao.findOrderInfo(orderId);
		if (order.getShopOrderStatus() == 1) {
			// 获取消费客户信息
			SysUser user = SysUser.dao.findByIdInCache(order.getShopOrderUser());
			String  shareholderId= Card.dao.findCardShareholderByUserId(user.getId());
			SysUser shareholder = SysUser.dao.findByIdInCache(shareholderId);//car表查shareholder id查对应人的微信openid

			// 获取分红股东openId
			String shareholderIdOpenId = shareholder.getUserOpenid();
			String notificatedShareholderUrl = "http://food.3kkk.xyz/carworld_weixin/dividend/getIncomeTodyCount?shareholderId="
					+ shareholder.getId();
			// 通知股东分红收入
			if (null != shareholderIdOpenId) {
				ApiResult apiResult = TemplateMsgApi.send(TemplateData.New().setTouser(shareholderIdOpenId)
						.setTemplate_id("8rEWeq2m5sFkS877q3N2p_5x-eCWzWszI3e_-TMwtlI") // 收益到账通知模板id
						.setUrl(notificatedShareholderUrl).add("first", "新的分红收益消息提示：\n")
						.add("keyword1", dividendGDTotal + "元", "#173177")
						.add("keyword2", "来自【" + user.getName() + "】的消费", "#173177")
						.add("keyword3", DateUtils.getCurrentTime(new Date()) + "\n", "#173177")
						.add("remark", "请在【个人中心】-->【分红收益】中查看详情", "#173177").build());
				System.out.println("通知股东：" + shareholder.getName() + "的消息： " + apiResult.getJson());
			}
		} else {
			System.out.println("订单" + orderId + "未结算,发送分红通知失败~");
			return;
		}
	}
}
